﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ScintillaNET;
using EkStudio.com.common.unit;
using EkStudio.com.bean.xml.WorkFlow.Node;

namespace EkStudio.com.ui.form
{
    public partial class FlowExpressionEditForm : XtraForm
    {

        private WflowExpressionElement express = null;
        private bool ROnly = false;

        public FlowExpressionEditForm(WflowExpressionElement exp,bool RdOnly)
        {
            InitializeComponent();
            express = exp;
            ROnly = RdOnly;
            InitForm();

        }

        private void InitForm()
        {
            #region 初始编辑器
            this.JScriptMemo.Margins[0].Width = 20;
            this.JScriptMemo.Margins[2].Type = MarginType.Symbol;
            this.JScriptMemo.Margins[2].Mask = Marker.MaskFolders;
            this.JScriptMemo.Margins[2].Sensitive = true;
            this.JScriptMemo.Margins[2].Width = 20;

            JScriptMemo.Markers[Marker.Folder].Symbol = MarkerSymbol.BoxPlus;
            JScriptMemo.Markers[Marker.FolderOpen].Symbol = MarkerSymbol.BoxMinus;
            JScriptMemo.Markers[Marker.FolderEnd].Symbol = MarkerSymbol.BoxPlusConnected;
            JScriptMemo.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
            JScriptMemo.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.BoxMinusConnected;
            JScriptMemo.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine;
            JScriptMemo.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner;

            JScriptMemo.StyleResetDefault();
            JScriptMemo.Styles[Style.Default].Font = "Consolas";
            JScriptMemo.Styles[Style.Default].Size = 10;
            JScriptMemo.StyleClearAll();


            // Configure the CPP (C#) lexer styles
            JScriptMemo.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
            JScriptMemo.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0); // Green
            JScriptMemo.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0); // Green
            JScriptMemo.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128); // Gray
            JScriptMemo.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
            JScriptMemo.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
            JScriptMemo.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
            JScriptMemo.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JScriptMemo.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JScriptMemo.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JScriptMemo.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
            JScriptMemo.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
            JScriptMemo.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;
            JScriptMemo.Lexer = Lexer.Cpp;

            // Set the keywords
            JScriptMemo.SetKeywords(0, "function abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe import virtual while");
            JScriptMemo.SetKeywords(1, "boolean var byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void");

            //JScriptMemo.SetKeywords(0, common.unit.Label.SQLKeys);
            if (express != null)
            {
                JScriptMemo.Text = express.Content;
            }
            

            #endregion
        }

        private void JScriptMemo_CharAdded(object sender, CharAddedEventArgs e)
        {
            if (e.Char == '.')
            {
                int currentPos = JScriptMemo.CurrentPosition;
                int wordStartPos = JScriptMemo.WordStartPosition(currentPos - 1, true);
                int lenEntered = currentPos - wordStartPos - 1;
                string text = JScriptMemo.GetTextRange(wordStartPos, lenEntered);
                if (text == "self")
                {
                    JScriptMemo.AutoCShow(0, JScriptAutoList.SelfAutoList);
                }
            }
        }

        private void EnteryButton_Click(object sender, EventArgs e)
        {
            if (ROnly) return;
            string content = JScriptMemo.Text.Trim();
            if (content != "" && content.IndexOf("function")>=0)
            {
                int fixedLeng = 8;
                int index = content.IndexOf("function");
                int end = content.IndexOf("(");
                string key = content.Substring(index + fixedLeng, end - index - fixedLeng);
                key = key.Trim();
                express.Key = key;
                express.Content = content;
            }
        }
    }
}